
/*
 * @Description: 分货调整明细DS
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2021-02-24 11:56:54
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-25 14:38:41
 */
import { FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import commonConfigStore from '@common/config/commonConfig';

import intl from 'utils/intl';
import moment from 'moment';
import { resetMonth } from '../utils/utilsFun';
const O2_OM = process.env.NODE_ENV === 'development' ? '/o2om-c' : commonConfigStore.O2_OM;
const O2_CM = process.env.NODE_ENV === 'development' ? '/o2cp' : commonConfigStore.O2_CM;
const adjustmentFormDS = () => ({
    autoQuery: false,
    selection: 'multiple',
    primaryKey: 'id',
    paging: false,
    autoCreate: true,
    transport: {
        read: config => {
            const { id } = config.data
            return {
                ...config,
                url: `${O2_OM}/v1/adjustments/${id}`,
                data: [],
                method: 'get',
            };
        },
        create: config => {
            const data = config.data[0];
            const { adjustmentMonth } = data;
            // 截掉月份后面的时间
            const submitData = resetMonth(data, adjustmentMonth, 'adjustmentMonth')
            const url = data.type === 'save' ? `${O2_OM}/v1/adjustments` : `${O2_OM}/v1/adjustments/submit`
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url,
                method: 'post',
            };
        },
        update: config => {
            const data = config.data[0];
            const { adjustmentMonth } = data;
            // 截掉月份后面的时间
            const submitData = resetMonth(data, adjustmentMonth, 'adjustmentMonth')
            const url = data.type === 'save' ? `${O2_OM}/v1/adjustments` : `${O2_OM}/v1/adjustments/submit`;
            const method =  data.type === 'save' ? 'put' : 'post'
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url,
                method,
            };
        },
    },
    fields: [
        {
            name:'id',
            type: FieldType.number,
        },
        {
            name: 'adjustmentCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.adjustmentCode').d('调整单号'),
        },
        {
            name: 'adjustmentReason',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.adjustmentReason').d('调整原因'),
            required: true,
        },
        {
            name: 'adjustmentMonth',
            type: FieldType.date,
            label: intl.get('dji.otc.om.view.field.adjustmentMonth').d('调整月份'),
            required: true,
            defaultValue: moment().format("YYYY MM"),
        },
        {
            name: 'adjustmentType',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.adjustmentType').d('调整类型'),
            required: true,
            lookupCode: 'DJISM.ADJUSTMENT_TYPE',
            defaultValue: 'RESULT'
        },
        {
            name: 'enclosureInfo',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.enclosureInfo').d('附件信息'),
        },
        {
            name: 'businessGroup',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.groupName').d('组别'),
            lookupCode: 'DJICM_GROUP',
            required: true,
        },
    ],
});

const adjustmentTableDS = () => ({
    autoCreate: false,
    autoQuery: false,
    paging: false,
    fields: [
        {
            name: 'productCodeObj',
            type: FieldType.object,
            label: intl.get('dji.otc.om.view.field.productName').d('产品'),
            required: true,
            ignore: 'always',
            lovCode: 'O2PCM.QUERY_MASTER_SKU_LIST',
            lovQueryAxiosConfig: (code, lovConfig) => {
                console.log(code, lovConfig);
                const { queryUrl } = lovConfig && lovConfig.originData || {};
                return {
                    url: queryUrl,
                    method: 'post',
                };
            }
        },
        {
            name: 'productCode',
            bind: 'productCodeObj.skuCode',
            type: FieldType.string,
        },
        {
            name: 'productName',
            bind: 'productCodeObj.skuName',
            type: FieldType.string,
        },
        {
            name: 'outAreaId',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.outAreaName').d('调出小区'),
            required: true,
            lookupUrl: `${O2_CM}/v1/cm-contract-permissions/get-area-info`,
        },
        {
            name: 'outRetailer',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.outRetailer').d('调出零售商'),
            required: true,
        },
        {
            name: 'outCustomerObj',
            type: FieldType.object,
            label: intl.get('dji.otc.om.view.field.outCustomerName').d('调出客户'),
            required: true,
            lovCode: 'DJIMKT_CUSTOMER_LIST_VIES',
            ignore: 'always',
        },
        {
            name: 'outCustomerCode',
            bind: 'outCustomerObj.customerTradeToken',
            type: FieldType.string,
        },
        {
            name: 'outCustomerName',
            bind: 'outCustomerObj.customerTradeName',
            type: FieldType.string,
        },
        {
            name: 'outWarehouse',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.outWarehouse').d('调出仓库'),
            required: true,
            lookupCode: 'DJIOM.RETURN_WAREHOUSE',
        },
        {
            name: 'intoAreaId',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.intoAreaName').d('调入小区'),
            required: true,
            lookupUrl: `${O2_CM}/v1/cm-contract-permissions/get-area-info`,
        },
        {
            name: 'intoRetailer',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.intoRetailer').d('调入零售商'),
            required: true,
        },
        {
            name: 'intoCustomerObj',
            type: FieldType.object,
            label: intl.get('dji.otc.om.view.field.intoCustomerName').d('调入客户'),
            required: true,
            lovCode: 'DJIMKT_CUSTOMER_LIST_VIES',
            ignore: 'always',
        },
        {
            name: 'intoCustomerCode',
            bind: 'intoCustomerObj.customerTradeToken',
            type: FieldType.string,
        },
        {
            name: 'intoCustomerName',
            bind: 'intoCustomerObj.customerTradeName',
            type: FieldType.string,
        },
        {
            name: 'intoWarehouse',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.intoWarehouse').d('调入仓库'),
            required: true,
            lookupCode: 'DJIOM.RETURN_WAREHOUSE'
        },
        {
            name: 'quantity',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.adjustQuantity').d('调整数量'),
            required: true,
        },
        {
            name: 'reason',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.adjustmentReason').d('调整原因'),
            required: true,
        },
        {
            name: 'quantityAfterOut',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.quantityAfterOut').d('调出小区调整后数量'),
            disabled: true,
        },
        {
            name: 'quantityAfterInto',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.quantityAfterInto').d('调入小区调整后数量'),
            disabled: true,
        },
    ],
});

const approvalDS = () => ({
    autoQuery: false,
    selection: false,
    paging: false,
    primaryKey: 'id',
    transport: {
        read: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/cle-charge-apply-workflow/flowPreviewRecords`,
                method: 'get',
            };
        },
    },
    fields: [
        { name: 'fileKey', type: FieldType.string },
        {
            name: 'NodeName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.currentSteps').d('当前步骤'),
        },
        {
            name: 'Approver',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.currentHandler').d('当前处理人'),
        },
        {
            name: 'ApprovalStatus',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.treatmentResults').d('处理结果'),
        },
        {
            name: 'ApprovalContent',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.handlingOpinions').d('处理意见'),
        },
        {
            name: 'CreateTime',
            type: FieldType.date,
            label: intl.get('dji.otc.om.view.field.lastHandleTime').d('最后处理时间'),
        },
    ],
});

const submitDateSet = () => ({
    autoQuery: false,
    transport: {
        submit: config => {
            const data = config.data[0];

            
            const { adjustmentMonth } = data[0];
            console.log(data[0]);
            
            // 截掉月份后面的时间
            const submitData = resetMonth(data[0], adjustmentMonth, 'adjustmentMonth')
            return {
                ...config,
                data: {
                    ...submitData,
                },
                url: `${O2_OM}/v1/adjustments/submit`,
                method: 'post',
            };
        },
    },
});

const reqAttachsDS = () => ({
    autoQuery: false,
    transport: {
        read: config => {
            return {
                ...config,
                url: `/hfle/v1/attachs`,
                method: 'get',
            };
        },
    },
});


export { adjustmentFormDS, adjustmentTableDS, approvalDS, submitDateSet, reqAttachsDS };
